<template>
	<view class="content">
		<view class="header">
			<view class="search-view">
				<input class="input" type="text" placeholder-style="color:#999999" v-model="keywords"
					placeholder="请输入关键词" />
				<image class="search-icon" src="../../static/images/icon_search.png"></image>
			</view>
			<view class="tap">
				<view class="tap-item" :class="tap == 0 ? 'active':''" data-index="0" @click="changeTap">
					热门资讯
				</view>
				<view class="tap-item" :class="tap == 1 ? 'active':''" data-index="1" @click="changeTap">
					藏友晒宝
				</view>
			</view>
		</view>
		<view class="list" v-if="tap==0">
			<informationItem v-for="(item,index) in datasource" :key="index" :data="item"></informationItem>
		</view>
		<view class="list magtop" v-if="tap==1">
			<bbsItem v-for="(item,index) in datasource" :key="index" :dataSource="item"></bbsItem>
		</view>
		<view class="list" v-if="datasource.length<=0">
			<noData></noData>
		</view>
		<noMoreData v-if="isBottom && datasource.length>0"></noMoreData>
		<image v-if="tap == 1" class="addTreasure" @click="addTreasure" src="../../static/images/wyysb.png"
			mode="widthFix"></image>
		<pageFooter v-if="footerSee"></pageFooter>
	</view>
</template>

<script>
	import informationItem from '../index/components/information-item.vue'
	import bbsItem from '../index/components/bbs-item.vue'
	import noMoreData from '@/components/noMoreData/index.vue'
	import noData from '@/components/noData/index.vue'
	import pageFooter from '@/components/pageFooter/index.vue'
	import {
		trendTreasure,
		articleList
	} from '@/api/peopleHide.js'
	export default {
		components: {
			informationItem,
			bbsItem,
			noData,
			noMoreData,
			pageFooter
		},
		data() {
			return {
				tap: 0,
				keywords: '',
				datasource: [],
				isBottom: false,
				page: 1,
				limit: 15,
				footerSee: false
			}
		},
		onLoad() {
			if (uni.getStorageSync('FOOTER_BAR')) {
				this.footerSee = true
				uni.hideTabBar()
			} else {

			}
		},
		onShow() {
			this.page = 1
			this.getDataSource()
		},
		onReachBottom() {
			this.page++
			this.getDataSource()
		},
		methods: {
			changeTap(e) {
				let index = e.currentTarget.dataset.index
				if (this.tap != index) {
					this.tap = index
					this.page = 1
					this.getDataSource()
				}
			},
			addTreasure() {
				uni.navigateTo({
					url: '../users/addTreasure/index'
				})
			},
			getDataSource() {
				if (this.tap == 1) {
					this.trendTreasure()
				} else {
					this.articleList()
				}
			},
			articleList() {
				articleList({
					page: this.page,
					limit: this.limit,
					keywords: this.keywords
				}).then(res => {
					let data = []
					if (this.page == 1) {
						data = res.data
					} else {
						data = this.datasource.concat(res.data)
					}
					this.datasource = data
					this.isBottom = res.data.length < this.limit ? true : false
				})
			},
			trendTreasure() {
				trendTreasure({
					page: this.page,
					limit: this.limit,
					keywords: this.keywords
				}).then(res => {
					let data = []
					if (this.page == 1) {
						data = res.data
					} else {
						data = this.datasource.concat(res.data)
					}
					this.datasource = data
					this.isBottom = res.data.length < this.limit ? true : false
				})
			}
		}
	}
</script>

<style lang="scss">
	page {
		background-color: #F4F5F6;
	}

	.header {
		display: flex;
		width: 100%;
		position: fixed;
		top: 0;
		left: 0;
		flex-direction: column;
		align-items: center;
		z-index: 99;
		background-color: #fff;

		.search-view {
			width: 690upx;
			height: 64upx;
			margin-top: 24upx;
			background-color: #F9F9F9;
			border-radius: 32upx;
			display: flex;
			align-items: center;
			justify-content: center;
			position: relative;

			.input {
				font-size: 30upx;
				text-align: center;
			}

			.search-icon {
				width: 28rpx;
				height: 28rpx;
				position: absolute;
				right: 20upx;
			}
		}

		.tap {
			width: 100%;
			height: 94upx;
			display: flex;
			align-items: center;
			justify-content: space-between;

			.tap-item {
				display: flex;
				align-items: center;
				justify-content: center;
				height: 100%;
				flex: 1;
				position: relative;
				font-size: 32upx;
				font-weight: 400;
			}

			.active {
				color: #0A77DE;
				font-weight: 500;
			}

			.active:after {
				position: absolute;
				content: '';
				bottom: 0;
				width: 50upx;
				background-color: #0A77DE;
				height: 2upx;
			}
		}
	}

	.list {
		display: flex;
		flex-direction: column;
		width: 100%;
		margin-top: 182upx;
		align-items: center;
	}

	.magtop {
		margin-top: 202upx;
	}

	.addTreasure {
		position: fixed;
		z-index: 9;
		width: 128upx;
		height: 128upx;
		right: 30upx;
		bottom: 480upx;
	}
</style>
